package com.kc.gb.device.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.google.common.collect.Maps;
import com.kc.gb.device.common.adapter.DeviceAdaptor;
import com.kc.gb.device.common.adapter.StationAdaptor;
import com.kc.gb.device.common.adapter.dto.basic.station.dto.BasicStationDTO;
import com.kc.gb.device.common.adapter.dto.basic.station.enums.LightType;
import com.kc.gb.device.common.adapter.dto.basic.station.enums.StationLiftType;
import com.kc.gb.base.dto.DeviceType;
import com.kc.gb.device.common.interfaces.IHardware;
import com.kc.gb.device.common.properties.DeviceProperties;
import com.kc.gb.device.common.utils.SpringHelper;
import com.kc.gb.device.service.HomeAdminService;
import com.kc.gb.device.tools.DeviceCodeTool;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.SocketException;
import java.util.Collection;
import java.util.Map;

@Service
public class IndexAdminServiceImpl implements HomeAdminService {

    @Resource
    DeviceAdaptor iDeviceAdaptor;

    @Resource
    StationAdaptor iStationAdaptor;

    @Resource
    DeviceProperties deviceProperties;

    @Override
    public String getDeviceCode() throws SocketException {
        return DeviceCodeTool.getDeviceCode();
    }

    @Override
    public String getApplicationStationCode(String deviceCode) throws Exception {
        return iDeviceAdaptor.getApplicationStationCode(deviceCode);
    }


    @Override
    public Map<String, Boolean> getDeviceEnabled(String deviceCode){
        Map<String, IHardware> beans = SpringHelper.getApplicationContext().getBeansOfType(IHardware.class);
        Collection<IHardware> devices = MapUtils.emptyIfNull(beans).values();
        Map<String, Boolean> map = Maps.newHashMap();
        CollectionUtils.emptyIfNull(devices).forEach(x->{
            map.put(x.getDeviceType().name(),true);
        });
        Map<String, Boolean> result = Maps.newHashMap();
        DeviceType[] values = DeviceType.values();
        if(ArrayUtil.isNotEmpty(values)){
            for(DeviceType deviceType : values){
                result.put(deviceType.name(), map.getOrDefault(deviceType.name(), false));
            }
        }
        return result;
    }


    public Map<String, Boolean> getAdminPageEnabled(String deviceCode){
        Map<String, IHardware> beans = SpringHelper.getApplicationContext().getBeansOfType(IHardware.class);
        Collection<IHardware> devices = MapUtils.emptyIfNull(beans).values();
        Map<String, Boolean> map = Maps.newHashMap();
        CollectionUtils.emptyIfNull(devices).forEach(x->{
            map.put(x.getDeviceType().name(),true);
        });
        Map<String, Boolean> result = Maps.newHashMap();
        DeviceType[] values = DeviceType.values();
        if(ArrayUtil.isNotEmpty(values)){
            for(DeviceType deviceType : values){
                result.put(deviceType.name(), map.getOrDefault(deviceType.name(), false));
            }
        }
        // 判定工作站的配置类型
        BasicStationDTO stationDTO = iStationAdaptor.getBasicStation(deviceProperties.getWarehouseId(),deviceCode);
        LightType type = stationDTO.getLightType();
        StationLiftType liftType = stationDTO.getLiftType();
        if(LightType.ptl.equals(type)){

        }
        if(LightType.strip.equals(type)){

        }
        return result;
    }


}
